<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class artistModule{
    public static function getArtists($_p,$_n){
        $SQLCommand="select * from artist where `delete` is null limit ".$_p*$_n.",$_n";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAllArtists(){
        $SQLCommand="select * from artist where `delete` is null order by `name`";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbums($_p,$_n){
        $SQLCommand="select * from album where `delete` is null order by `subject` limit ".$_p*$_n.",$_n";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbums_search($_key,$_bugs,$_subject,$_p,$_n){
        $SQLCommand="select * from album where  `delete` is null 
                    ".((isset($_subject)||$_subject!="")?("and album.`subject` like '%".setstringhtml($_subject)."%'"):"")."
                    ".((isset($_key)||$_key!="")?("and album.`key` like '%".setstringhtml($_key)."%'"):"")."
                    ".((isset($_bugs)||$_bugs!="")?("and album.`key_bugs` like '%".setstringhtml($_bugs)."%'"):"")."
                    order by `subject`
                    limit ".$_p*$_n.",$_n";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) while ($row=$data->fetch_array()){
                array_push($dt, $row);
        }else        return null;
        return $dt;
    }
    public static function getAlbums_search_numpage($_key,$_bugs,$_subject,$_n){
        $SQLCommand="select count(*) as numrow from album where  `delete` is null
                    ".((isset($_subject)||$_subject!="")?("and album.`subject` like '%".setstringhtml($_subject)."%'"):"")."
                    ".((isset($_key)||$_key!="")?("and album.`key` like '%".setstringhtml($_key)."%'"):"")."
                    ".((isset($_bugs)||$_bugs!="")?("and album.`key_bugs` like '%".setstringhtml($_bugs)."%'"):"")."
                    ";
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null ) {
            $row=$data->fetch_array();
            return ($row["numrow"]%$_n==0)?(int)($row["numrow"]/$_n):((int)($row["numrow"]/$_n)+1);
        }
        return 0;
    }
    public static function getAlbum($_key){
        $SQLCommand="select * from album where  `delete` is null
                    and `key`='$_key'
                    ";
        $data=DataProvider::executeSQL($SQLCommand);
        $dt=array();
        if($data!=null) {
            $row=$data->fetch_array();
            return $row;;
        }
        return null;

    }
    public static function updateAlbum($_key,$_artist,$_type,$_subject,$_track,$_image,$_release,$_content){
        //$_subject=str_replace("\"", "\\\"", str_replace("'", "\'", $_subject));
        //$_image=str_replace("\"", "\\\"", str_replace("'", "\'", $_image));
        //$_content=str_replace("\"", "\\\"", str_replace("'", "\'", $_content));
        $SQLCommand="
            update `album` set
                `artist`=$_artist,
                `type`=$_type,
                `subject`=$_subject,
                `track_total`=$_track,
                `image`=$_image,
                `release`=$_release,
                `content`=$_content,
                `update`=now()
            where `key`=$_key
        ";
        $n=DataProvider::executenoneSQL($SQLCommand);
        if($n==1) {
            return true;
        }
        return false;

    }
    public static function checkArtistExist($_key){
        $SQLCommand="
            select count(*) as numrow from artist where `delete` is null and `key`=$_key
        ";
        $data=DataProvider::executeSQL($SQLCommand);
        if($data!=null ) {
            $row=$data->fetch_array();
            if($row["numrow"]==1)                return true;
        }
        return false;

    }
}
if(isset($_REQUEST["SO_action"])){
     switch ($_REQUEST["SO_action"]){
        case "getalbum":
            include '../JO_20_SO/SO_032_albumOBJ.php';
            break;
        case "loadeditalbum":
            include '../JO_20_SO/SO_032_editalbum.php';
            break;
        case "savealbum":
            $_key=isset($_REQUEST["palbum"])?($_REQUEST["palbum"]==""?"null":$_REQUEST["palbum"]):"null";
            $_artist=isset($_REQUEST["partist"])?($_REQUEST["partist"]==""?"null":$_REQUEST["partist"]):"null";
            $_type=isset($_REQUEST["ptype"])?($_REQUEST["ptype"]==""?"null":("'".$_REQUEST["ptype"]."'")):"null";
            $_subject=isset($_REQUEST["psubject"])?($_REQUEST["psubject"]==""?"null":("'".setstringhtml($_REQUEST["psubject"])."'")):"null";
            $_track=isset($_REQUEST["ptrack"])?($_REQUEST["ptrack"]==""?"null":"'".setstringhtml($_REQUEST["ptrack"])."'"):"null";
            $_image=isset($_REQUEST["pimg"])?($_REQUEST["pimg"]==""?"null":("'".setstringhtml($_REQUEST["pimg"])."'")):"null";
            $_release=isset($_REQUEST["prelease"])?($_REQUEST["prelease"]==""?"null":("'".setstringhtml($_REQUEST["prelease"])."'")):"null";
            $_content=isset($_REQUEST["pcontent"])?($_REQUEST["pcontent"]==""?"null":("'".setstringhtml($_REQUEST["pcontent"])."'")):"null";
            $nresult="";
            $serrormsg="";
            $roke=artistModule::checkArtistExist($_artist);
            if($roke){
                $n=artistModule::updateAlbum($_key,$_artist,$_type,$_subject,$_track,$_image,$_release,$_content);
                if($n){
                    $nresult="1";
                    $serrormsg="Update susscess.";
                }else{
                    $nresult="0";
                    $serrormsg="Update not finish.Please check input data.";
                }
            }else{
                $nresult="0";
                $serrormsg="Artist key does'n exist.";
            }
            $member = array('nresult' => $nresult
                            ,'serrormsg' => $serrormsg
                            );
            echo json_encode($member);
            break;
        case "getalbuminfo":
            $_key=isset($_REQUEST["palbum"])?($_REQUEST["palbum"]==""?"null":$_REQUEST["palbum"]):"null";
            $rowalbum=artistModule::getAlbum($_key);

            $dataresult = array('ssubject' => (isset($rowalbum["subject"])?$rowalbum["subject"]:"")
                            ,'dinsert' => (isset($rowalbum["insert"])?$rowalbum["insert"]:"")
                            ,'dupdate' => (isset($rowalbum["update"])?$rowalbum["update"]:"")
                            );
            echo json_encode($dataresult);
            break;
     }
}
?>
